package com.shmn.gongjiajumain.service.impl;

import com.shmn.gongjiajumain.service.WorkcellService;
import com.shmn.gongjiajumain.utils.DateUtil;
import com.shmn.gongjiajumain.vo.people.PeopleTableVo;
import com.shmn.gongjiajumain.vo.people.PeopleUpdateVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shmn.common.utils.PageUtils;
import com.shmn.common.utils.Query;

import com.shmn.gongjiajumain.dao.UserDao;
import com.shmn.gongjiajumain.entity.UserEntity;
import com.shmn.gongjiajumain.service.UserService;

import javax.annotation.Resource;

@Slf4j
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {
    @Resource
    private WorkcellService workcellService;

    @Override
    public PageUtils queryPage1(UserEntity user) {

        Map<String, Object> params = new HashMap<>();
        params.put("page",user.getPageIndex());
        params.put("limit",user.getPageSize());
        log.info("map:{}",params);
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        if (user.getId() != null) wrapper.like("id",user.getId());
        if (user.getName() != null) wrapper.like("name",user.getName());
        if (user.getWorkcellId() != null) wrapper.eq("workcell_id",user.getWorkcellId());
        if (user.getStartToEndTime() != null){
            List<String> startToEndTime = user.getStartToEndTime();
            String start = startToEndTime.get(0);
            String end = startToEndTime.get(1);
            if (start != null && end != null) wrapper.between("entry_date",start,end);
            else if (start != null) wrapper.ge("entry_date",start);
            else wrapper.le("entry_date",end);
        }
        IPage<UserEntity> page = this.page(new Query<UserEntity>().getPage(params), wrapper);
        PageUtils pageUtils = new PageUtils(page);
        List<PeopleTableVo> peopleTableVos = new ArrayList<>();
        pageUtils.getList().forEach(item -> {
            PeopleTableVo peopleTableVo = new PeopleTableVo();
            BeanUtils.copyProperties(item, peopleTableVo);
            Class<UserEntity> userEntityClass = UserEntity.class;
            Integer workCellId = null;
            try {
                Field field = userEntityClass.getDeclaredField("workcellId");
                field.setAccessible(true);
                workCellId = (Integer)field.get(item);
            } catch (Exception e) {
                log.error("报错信息{}",e.getMessage());
            }
            String workCell = workcellService.getWorkCellById(workCellId);
            peopleTableVo.setWorkCell(workCell);
            log.info("peopleTableVo:{}",peopleTableVo);
            peopleTableVos.add(peopleTableVo);
        });
        pageUtils.setList(peopleTableVos);
        return pageUtils;
    }

    @Override
    public PeopleUpdateVo getByInfoId(String id) {
        UserEntity userEntity = baseMapper.selectById(id);
        PeopleUpdateVo peopleUpdateVo = new PeopleUpdateVo();
        BeanUtils.copyProperties(userEntity,peopleUpdateVo);
        peopleUpdateVo.setEntryDate(DateUtil.formatDateJust(userEntity.getEntryDate()));
        peopleUpdateVo.setWorkCell(workcellService.getWorkCellById(userEntity.getWorkcellId()));
        return peopleUpdateVo;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
        );
        PageUtils pageUtils = new PageUtils(page);
        List<PeopleTableVo> peopleTableVos = new ArrayList<>();
        pageUtils.getList().forEach(item -> {
            PeopleTableVo peopleTableVo = new PeopleTableVo();
            BeanUtils.copyProperties(item, peopleTableVo);
            Class<UserEntity> userEntityClass = UserEntity.class;
            Integer workCellId = null;
            try {
                Field field = userEntityClass.getDeclaredField("workcellId");
                field.setAccessible(true);
                workCellId = (Integer)field.get(item);
            } catch (Exception e) {
            }
            String workCell = workcellService.getWorkCellById(workCellId);
            peopleTableVo.setWorkCell(workCell);
            peopleTableVos.add(peopleTableVo);
        });
        pageUtils.setList(peopleTableVos);
        return pageUtils;
    }

}